home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Day Cry
/
Day Cry CD.bin
/
oh_towns
/
taropyon
/
splib
/
splib.lzh
/
PRG
/
LHX
/
APPEND.C
next >
Wrap
C/C++ Source or Header
|
1992-12-21
|
6KB
|
282 lines
/***********************************************************
append.c -- append to archive
***********************************************************/
#include "lh386.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
#include <dos.h>
#include "lh.h"
#include "intrface.h"
#ifdef __HIGHC__
# pragma On(Align_labels);
#endif
#ifdef __HIGHC__
extern long filesize(FILE * fp);
#endif
#define MAX_INDICATOR_COUNT 64
long indicator_count;
long indicator_threshold;
extern int compress_method;
extern long int reading_size;
extern unsigned short dicbit;
struct interfacing interface;
void start_indicator(char *name, long size, char *msg, long def_indicator_threshold)
{
long i;
int m;
m = MAX_INDICATOR_COUNT - strlen(name);
if (m < 0)
m = 3; /* (^_^) */
#ifdef __MSDOS__
LHX_fprintf(stderr, "\r%-12s - %s : ", name, msg);
#else
LHX_fprintf(stderr, "\r%s\t- %s : ", name, msg);
#endif
indicator_threshold =
((size + (m * def_indicator_threshold - 1)) /
(m * def_indicator_threshold) *
def_indicator_threshold);
if (indicator_threshold)
i = ((size + (indicator_threshold - 1)) / indicator_threshold);
else
i = 0;
while (i--)
LHX_fputc('.', stderr);
indicator_count = 0;
#ifdef __MSDOS__
LHX_fprintf(stderr, "\r%-12s - %s : ", name, msg);
#else
LHX_fprintf(stderr, "\r%s\t- %s : ", name, msg);
#endif
fflush(stderr);
reading_size = 0L;
}
void finish_indicator2(char *name, char *msg, int pcnt)
{
if (pcnt > 100)
pcnt = 100; /* (^_^) */
#ifdef __MSDOS__
LHX_fprintf(stderr, "\r%-12s - %s(%d%%)\n", name, msg, pcnt);
#else
LHX_fprintf(stderr, "\r%s\t- %s(%d%%)\n", name, msg, pcnt);
#endif
indicator_threshold = 0;
fflush(stderr);
}
void finish_indicator(char *name, char *msg)
{
#ifdef __MSDOS__
LHX_fprintf(stderr, "\r%-12s - %s\n", name, msg);
#else
LHX_fprintf(stderr, "\r%s\t- %s\n", name, msg);
#endif
indicator_threshold = 0;
fflush(stderr);
}
void copylzh(void)
{
update_arcstamp();
writehdr();
copyfile(file1, file2, hpb.packed, 0);
}
static int isarc(char *name)
{
static char arc[7][3] =
{" ", "zoo", "arc", "pak", "zip", "lzs", "lzh"};
char *p;
int i;
p = name + strlen(name) - 4;
if (p < name || *p != '.')
return 0;
p++;
memcpy(arc[0], p, 3);
for (i = 6; memicmp(arc[i], p, 3); i--);
return i;
}
void freeze(char *path, char *name)
{
static int method = -1;
hpb.pathname = name;
hpb.filename = getfilename(name);
file3 = myropen(path);
if (method < 0)
{
method = 5;
if (flg_o)
method = 1;
if (flg_z == 1)
method = 0;
if (method > 0)
method = encode_alloc(method);
}
hpb.attr = 0x20;
if (flg_a)
{
#ifdef __HIGHC__
unsigned int attr;
_dos_getfileattr(path, &attr);
hpb.attr = attr;
#else
_dos_getfileattr(path, (unsigned int *) &hpb.attr);
#endif
}
hpb.utc = getfiletime(file3);
hpb.level = flg_h;
hpb.info = 0;
#ifndef __HIGHC__
hpb.original = hpb.packed = filelength(fileno(file3));
#else
hpb.original = hpb.packed = filesize(file3);
#endif
strcpy(hpb.method, "-lh5-");
interface.method = method;
if (hpb.original == 0 || flg_z == 2 && isarc(hpb.filename))
interface.method = 0;
if (interface.method > 0)
{
makehdr();
writehdr();
interface.infile = file3;
interface.outfile = file2;
interface.original = hpb.original;
start_indicator(hpb.filename, hpb.original, "Freezing", 1 << dicbit);
encode(&interface);
if (interface.packed >= interface.original)
{
rewind(file3);
fseek(file2, hpb.currentpos, SEEK_SET);
interface.method = 0;
}
hpb.packed = interface.packed;
}
hpb.method[3] = interface.method + '0';
if (interface.method == 0)
{
makehdr();
writehdr();
copyfile(file3, file2, hpb.original, 1);
}
update_arcstamp();
hpb.filecrc = crc;
adjusthdr();
fclose(file3);
finish_indicator2(hpb.filename, "Frozen", ratio(hpb.packed, hpb.original, 2));
}
static void freeze_fb(struct fb * fp)
{
char *p;
char *path;
path = fp->fname;
p = e_malloc(strlen(path) + 1);
strcpy(p, path);
freeze(p, p + (fp->cpos - path));
free(p);
}
int append(void)
{
struct fb *fp;
if ((fp = searchfile(hpb.pathname)) != NULL
&& (flg_c || fp->time > hpb.utc))
{
freeze_fb(fp);
return 1;
} else
{
copylzh();
return 0;
}
}
int endappend(void)
{
struct fb *fp;
int modified;
modified = 0;
for (fp = fbuf; fp != NULL; fp = fp->nxt)
{
if (fp->used == 0)
{
freeze_fb(fp);
modified++;
}
}
return modified;
}
void deletefiles(void)
{
struct fb *fp;
for (fp = fbuf; fp != NULL; fp = fp->nxt)
{
strcpy(work, fp->fname);
remove(work);
}
}
int freshen(char *bdir)
{
char *path, *name;
struct find_t srchbuf;
int flag;
path = e_malloc(strlen(bdir) + strlen(hpb.pathname) + 1);
strcpy(path, bdir);
if (*hpb.pathname == DELIM)
{
if (path[1] == ':')
{
path[2] = '\0';
} else
{
*path = '\0';
}
}
name = path + strlen(path);
strcpy(name, hpb.pathname);
flag = !_dos_findfirst(path, flg_a ? 0x07 : 0, &srchbuf);
if (flag)
{
if (!flg_c)
{
if (hpb.utc >= dos2unix((struct ftime *) & (srchbuf.wr_time)))
flag = 0;
}
}
if (flag)
{
freeze(path, name);
} else
{
copylzh();
}
free(path);
return flag;
}